package com.fzy.leetcode.editor.cn;
//2023-03-15 21:17:15
//输入某二叉树的前序遍历和中序遍历的结果，请构建该二叉树并返回其根节点。 
//
// 假设输入的前序遍历和中序遍历的结果中都不含重复的数字。 
//
// 
//
// 示例 1: 
// 
// 
//Input: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
//Output: [3,9,20,null,null,15,7]
// 
//
// 示例 2: 
//
// 
//Input: preorder = [-1], inorder = [-1]
//Output: [-1]
// 
//
// 
//
// 限制： 
//
// 0 <= 节点个数 <= 5000 
//
// 
//
// 注意：本题与主站 105 题重复：https://leetcode-cn.com/problems/construct-binary-tree-from-
//preorder-and-inorder-traversal/ 
//
// Related Topics 树 数组 哈希表 分治 二叉树 👍 1026 👎 0

import java.util.HashMap;
import java.util.Map;

class ZhongJianErChaShuLcof {
    public static void main(String[] args) {
        //创建该题目的对象方便调用
        Solution solution = new ZhongJianErChaShuLcof().new Solution();
    }
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int x) { val = x; }
    }
    //leetcode submit region begin(Prohibit modification and deletion)
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    Map<Integer,Integer> map;
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        map = new HashMap<>();
        for(int i = 0;i<inorder.length;i++){
            map.put(inorder[i],i);
        }
        return findNode(preorder,0,preorder.length,inorder,0,inorder.length);
    }
    public TreeNode findNode(int[] preorder,int preStart,int preEnd,
                             int[] inorder,int inStart,int inEnd){

        if(preStart >= preEnd || inStart >= inEnd){
            return null;
        }

        int rootIdx = map.get(preorder[preStart]);
        TreeNode root = new TreeNode(inorder[rootIdx]);
        int lenOfLeft = rootIdx - inStart;
        root.left = findNode(preorder,preStart+1,preStart+lenOfLeft+1,inorder,inStart,inEnd);
        root.right = findNode(preorder,preStart+lenOfLeft+1,preEnd,inorder,rootIdx+1,inEnd);

        return root;
    }
}
//leetcode submit region end(Prohibit modification and deletion)

}